package util;

import bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @description: mybatis工具类
 * @author: apple
 * @date:  2022/2/19 5:13 下午
 * @version 1.0
 **/
public class MyBatisUtil {

    // 工厂类
    private static SqlSessionFactory factory = null;

    static {
        String resource = "mybatis.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            factory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("加载工厂类成功");
    }

    public static SqlSession getSqlSesstion() {
        SqlSession session = null;
        if (factory != null) {
            session = factory.openSession();
        }
       return  session;
    }

}
